package com.polylink.sys.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.polylink.base.Page;
import com.polylink.sys.bean.User;
import com.polylink.sys.dao.IUserDao;
import com.polylink.sys.service.IUserService;

/**
 *@author xiliang.xiao
 *@date 2015年3月23日 上午9:03:48
 *
 **/
@Service
public class UserService implements IUserService{
	@Autowired
	private IUserDao userDao;

	@Override
	public String save(User user) {
		String code = validate(user);
		if(code==null){
			int i = 0;
			if(user.getId()!=null){
				i = userDao.update(user);
			}else{
				user.setPsw(UserPasswordEncrypt.encrypt(user.getPsw()));
				i = userDao.add(user);
			}
			if(i>0){
				return "success";
			}
			return "fail";
		}else{
			return code;
		}
	}

	/**
	 * 验证是否可以修改用户
	 * 用户名不可重复
	 * @param user
	 * @return
	 */
	private String validate(User user) {
		boolean exist = checkNameExist(user);
		if(exist){
			return "err1";
		}
		return null;
	}

	/**
	 * 验证用户名是否存在
	 * @param user
	 * @return
	 */
	private boolean checkNameExist(User user) {
		User u = findUserByUsername(user.getName());
		if(u==null){
			return false;
		}else{
			if(user.getId()!=null){
				if(user.getId().intValue()==u.getId().intValue()){
					return false;
				}else{
					//修改时如果两个用户ID不一至则存在
					return true;
				}
			}else{
				//新增时只要数据库存在即存在
				return true;
			}
		}
	}

	@Override
	public Page list(Page p) {
		List<User> list = userDao.list(p);
		int count = userDao.count(p);
		p.setRows(list);
		p.setTotal(count);
		return p;
	}

	@Override
	public boolean delete(String id) {
		int i = userDao.delete(id);
		if(i>0){
			return true;
		}
		return false;
	}

	@Override
	public User findUserByUsername(String username) {
		return userDao.findUserByUsername(username);
	}

	@Override
	public List<Map<String, Object>> findUserFunctionByUsername(String username) {
		return userDao.findUserFunctionByUsername(username);
	}

	@Override
	public List<Map<String, Object>> getRoleOutUser(String id) {
		return userDao.getRoleOutUser(id);
	}

	@Override
	public void userRightSave(String userId,String roleId) {
		Set<String> roleIds = new HashSet<String>(Arrays.asList(roleId.split(",")));
		List<Map<String,Object>> urList = userDao.getUserRole(userId);
		//要删除的关联
		List<String> removeUr = new ArrayList<String>();
		if(urList!=null&&urList.size()>0){
			for(Map<String,Object> map:urList){
				String rId = map.get("role_id").toString();
				if(roleIds.contains(rId)){
					//移除不需要更新的，剩下为需要添加的
					roleIds.remove(rId);
				}else{
					//需要删除的
					removeUr.add(rId);
				}
			}
		}
		if(roleIds!=null&&roleIds.size()>0){
			for(String id:roleIds){
				if(StringUtils.isNotBlank(id)){
					userDao.addUserRole(id,userId);
				}
			}
		}
		if(removeUr!=null&&removeUr.size()>0){
			for(String id:removeUr){
				if(StringUtils.isNotBlank(id)){
					userDao.delUserRole(id,userId);
				}
			}
		}
	}

	@Override
	public boolean resetPsw(String id, String psw) {
		int i = userDao.resetPsw(id,UserPasswordEncrypt.encrypt(psw));
		if(i>0){
			return true;
		}else{
			return false;
		}
	}

}
